import collections
class Solution(object):
    def minCharacters(self, a, b):
        n1 = len(a)
        n2 = len(b)
        c1 = collections.Counter(a)
        c2 = collections.Counter(b)
        c = c1 + c2
        max_val = max(c.values())
        method3 = (n1 + n2) - max_val
        pre_a = [0] * 26
        pre_b = [0] * 26
        for key in c1:
            pre_a[ord(key) - ord('a')] = c1[key]
        for key in c2:
            pre_b[ord(key) - ord('a')] = c2[key]
        for i in range(1, 26):
            pre_a[i] += pre_a[i - 1]
            pre_b[i] += pre_b[i - 1]
        method1 = float('inf')
        for i in range(25):
            p1 = n1 - pre_a[i] + pre_b[i]
            p2 = pre_a[i] + n2 - pre_b[i]
            method1 = min(method1, p1, p2)
        return min(method1, method3)
